<?php
namespace App\Services\Admin;

use App\Models\AreaModel;
use App\Repositories\Admin\CategoryRepository;
use App\Repositories\Main\ComCostRepository;
use App\Repositories\Main\ComCostTypeRepository;
use App\Repositories\Admin\CompanyRepository;
use App\Repositories\Admin\UserImgTmpRepository;
use App\Repositories\Admin\UserRepository;

class CompanyService
{
    private $service;
    private $user;
    private $cost;
    private $costType;

    public function __construct()
    {
        $this->service = new CompanyRepository();
        $this->user = new UserRepository();
        $this->cost = new ComCostRepository();
        $this->costType = new ComCostTypeRepository();
    }

    /**
     * ------------------------------------------
     * 企业信息
     * ------------------------------------------
     */

    public function getIndex()
    {
        return $this->service->getIndexList();
    }

    /**
     * 通过类型，获取工厂/公司
     */
    public function getWebIndexByCate($cate)
    {
        return $this->service->getWebIndexByCate($cate);
    }

    /**
     * APP工厂列表
     */
    public function getAppFacs()
    {
        $models = $this->service->getAppFacs();
        if (!count($models)) { return ''; }
        $dataArr = array();
        foreach ($models as $k=>$model) {
            $user = $this->user->getShowOne($model->cid);
            $dataArr[$k]['comName'] = $model->com_name ? $model->com_name : '测试公司';
            $dataArr[$k]['cityName'] = $model->com_address ? $model->com_address : '测试地址';
            $dataArr[$k]['cusLike'] = $model->cus_like;
            $dataArr[$k]['quality'] = $model->quality;
            $dataArr[$k]['uname'] = $user ? $user->name : '发布人';
            $dataArr[$k]['id'] = $model->id;
        }
        return $dataArr;
    }

    /**
     * 工厂类型
     */
    public function getFacTypes()
    {
        $cateService = new CategoryService();
        $facTypes = $cateService->getSubs(1);
        $dataArr = array();
        static $key = 0;
        foreach ($facTypes as $k=>$facType) {
            if ($k%2!=0) {
                $dataArr[$key][0]['id'] = $facType->id;
                $dataArr[$key][0]['name'] = $facType->name;
                $key ++;
            } else {
                $dataArr[$key][1]['id'] = $facType->id;
                $dataArr[$key][1]['name'] = $facType->name;
            }
        }
        return $dataArr;
    }

    /**
     * 工厂类型
     */
    public function getFacTypeName($facType)
    {
        $cateService = new CategoryService();
        $model = $cateService->getOne($facType);
        return $model ? $model->name : '';
    }

    /**
     * 业务类型
     */
    public function getOutTypes($cate)
    {
        $cateId = $cate==1 ? 108 : 109;
        $cateService = new CategoryService();
        return $cateService->getSubs($cateId);
    }

    public function add($uid,$data)
    {
        $model = $this->service->getShowOneByUid($uid);
//        if ($data['legal_idcard_imgs'] &&
//            $imgs = explode('|',$data['legal_idcard_imgs'])) {
//            foreach ($imgs as $img) {
//                $this->setImgTmp($img);
//            }
//        }
        if ($model) {
            return $this->service->setModifyByUid($data,$uid);
        } else {
            return $this->service->setAdd($data);
        }
    }

    public function modify($data,$uid)
    {
        return $this->service->setModify($data,$uid);
    }

    public function getOne($id)
    {
        return $this->service->getShowOne($id);
    }

    public function getOneByUid($uid)
    {
        return $this->service->getShowOneByUid($uid);
    }

    /**
     * 对象转数组
     */
    public function getOneArr($id)
    {
        $model = $this->service->getShowOne($id);
        $model->cateName = $this->service->getCateNameByCate($model->cate);
        $model->sexName = $this->service->getSexNameBySex($model->sex);
        if ($model->legal_idcard_imgs) {
            $idcards = explode('||',$model->legal_idcard_imgs);
            $model->idcadImg1 = $idcards[0];
            $model->idcadImg2 = $idcards[1];
            $model->idcadImg3 = $idcards[2];
        } else {
            $model->idcadImg1 = '';
            $model->idcadImg2 = '';
            $model->idcadImg3 = '';
        }
        return array(
            'cateName' => $model->cateName,
            'legalName' => $model->legal_name,
            'legalSexName' => $model->sexName,
            'legalMobile' => $model->legal_mobile,
            'legalIdcard' => $model->legal_idcard,
            'idcardImg1' => $model->idcadImg1,
            'idcardImg2' => $model->idcadImg2,
            'idcardImg3' => $model->idcadImg3,
            'comName' => $model->com_name,
            'comTel' => $model->com_tel,
            'comFax' => $model->com_fax,
            'comAddress' => $model->com_address,
            'comCityName' => $model->com_city,
            'cusLike' => $model->cus_like,
            'quality' => $model->quality,
            'pact' => $model->pact,
            'outType' => $model->out_type,
            'ageRange' => $model->age_range,
            'detail' => $model->detail,
        );
    }

    /**
     * 用户类型名称
     */
    public function getCateName($cate)
    {
        return $this->service->getCateNameByCate($cate);
    }

    /**
     * 通过城市，获取ID
     */
    public function getCityIdByName($cityName)
    {
        $model = AreaModel::where('name',$cityName)->first();
        if (!$model) {
            $model2 = AreaModel::where('name','LIKE',$cityName.'%')->first();
            if($model2){
                return $model2->id;
            }
            return 0;
        }
        return $model->id;
    }

    /**
     * ------------------------------------------
     * 企业信息
     * ------------------------------------------
     */





    /**
     * ------------------------------------------
     * 企业可选信息
     * ------------------------------------------
     */

    /**
     * 企业可选，获取
     */
    public function getOptionLicence($uid)
    {
        return $this->service->getOptionLicenceByuid($uid);
    }

    /**
     * 企业可选，获取2数组
     */
    public function getOptionLicenceArr($uid)
    {
        return $this->service->getOptionLicenceArrByuid($uid);
    }

    /**
     * 企业可选，设置
     */
    public function setOptionLicence($uid,$fileKey,$filePath)
    {
        $keys = [
            1 => 'photos','idcards','graduates','honors','marrys','drives',
            11 => 'zzjgdmzs','yyzzs','swdjzs','rents','licencess','honorss','houses',
        ];
        $resFiles = $this->service->getOptionLicenceArrByuid($uid);
        $dataArr = array();
        if (!$resFiles['legals'] || !$resFiles['coms']) {
            foreach ($keys as $k=>$key) {
                $keyPre = $k>10?'coms':'legals';
                if ($fileKey==$key) {
                    $dataArr[$keyPre][$key][] = $filePath;
                } else {
                    $dataArr[$keyPre][$key] = array();
                }
            }
            $this->service->setOptionLicenceByuid($uid,0,$dataArr['legals'],$dataArr['coms']);
            return true;
        }
        foreach ($keys as $k2=>$key) {
            $keyPre = $k2>10?'coms':'legals';
            $dataArr[$keyPre][$key] = $resFiles[$keyPre][$key];
            if ($fileKey==$key) {
                $dataArr[$keyPre][$key][] = $filePath;
            }
        }
        $this->service->setOptionLicenceByuid($uid,1,$dataArr['legals'],$dataArr['coms']);
        return true;
    }

    /**
     * 企业认证，链接设置
     */
    public function setComFileLink($uid,$legals,$coms)
    {
        foreach ($legals as $legal) {
            if ($legal) {
                foreach ($legal as $v) { $this->setImgTmp($v); }
            }
        }
        foreach ($coms as $com) {
            if ($com) {
                foreach ($com as $v2) { $this->setImgTmp($v2); }
            }
        }
        $this->service->setOptionLicenceByuid($uid,1,$legals,$coms);
        return true;
    }

    /**
     * ------------------------------------------
     * 企业可选信息
     * ------------------------------------------
     */





    /**
     * ------------------------------------------
     * 产品
     * ------------------------------------------
     */

    /**
     * 产品列表，获取
     */
    public function getProductList($pid)
    {
        $cateModel = new CategoryRepository();
        return $cateModel->getSubList($pid);
    }

    /**
     * 产品数据，获取
     */
    public function getOptionPro($uid)
    {
        return $this->service->getOptionProList($uid);
    }

    /**
     * 产品，设置
     */
    public function setOptionPro($uid,$data)
    {
        return $this->service->setOptionProList($uid,$data);
    }

    /**
     * ------------------------------------------
     * 产品
     * ------------------------------------------
     */





    /**
     * ------------------------------------------
     * 设备
     * ------------------------------------------
     */

    /**
     * 工厂设备，获取
     */
    public function getEquipList()
    {
        $cateModel = new CategoryRepository();
        return $cateModel->getSubList(88);
    }

    /**
     * 通过设备ID。获取设备名称
     */
    public function getEquipById($equipId)
    {
        $cateModel = new CategoryRepository();
        return $cateModel->getCateNameById($equipId);
    }

    /**
     * 工厂设备，获取
     */
    public function getOptionEquip($uid)
    {
        return $this->service->getOptionEquipList($uid);
    }

    /**
     * 工厂设备，设置
     */
    public function setOptionEquip($uid,$data)
    {
        return $this->service->setOptionEquip($uid,$data);
    }

    /**
     * ------------------------------------------
     * 设备
     * ------------------------------------------
     */





    /**
     * ------------------------------------------
     * 产量产能
     * ------------------------------------------
     */

    /**
     * 产量产能，获取
     */
    public function getOptionYield($uid)
    {
        return $this->service->getOptionYield($uid);
    }

    /**
     * 产量产能，设置
     */
    public function setOptionYield($uid,$data)
    {
        return $this->service->setOptionYield($uid,$data);
    }

    /**
     * ------------------------------------------
     * 产量产能
     * ------------------------------------------
     */





    /**
     * -------------------------------------
     * 工厂环境
     * -------------------------------------
     */

    /**
     * 环境，获取
     */
    public function getFacEnv($uid)
    {
        return $this->service->getFacEnvList($uid);
    }

    /**
     * 环境，设置
     */
    public function setFacEnv($uid,$fileKey='managers',$filePath='')
    {
        $dataArr = array();
        $keys = array('managers','facManagers','finances','guards',
            'shapes','raws','techs','works','backs','cuts','dorms','samples',);
        $resFiles = $this->service->getFacEnvJson($uid);
        if (!$resFiles) {
            foreach ($keys as $key) {
                if ($fileKey==$key) {
                    $dataArr[$key][] = $filePath;
                } else {
                    $dataArr[$key] = array();
                }
            }
            $this->service->setFacEnvByUid($uid,$dataArr);
            return true;
        }
        $resFiles = json_decode($resFiles,true);
        foreach ($keys as $key) {
            $dataArr[$key] = $resFiles[$key];
            if ($fileKey==$key) {
                $dataArr[$key][] = $filePath;
            }
        }
        $this->service->setFacEnvByUid($uid,$dataArr);
        return true;
    }

    /**
     * -------------------------------------
     * 工厂环境
     * -------------------------------------
     */





    /**
     * -------------------------------------
     * 公司环境
     * -------------------------------------
     */

    /**
     * 环境，获取
     */
    public function getComEnv($uid)
    {
        return $this->service->getFacEnvList($uid);
    }

    /**
     * 环境，设置
     */
    public function setComEnv($uid,$fileKey='managers',$filePath='')
    {
        $dataArr = array();
        $keys = array(
            'managers','manager2s','directors','handles','finances',
            'officers','sales','expands','designs','buyers','sellers',
            'produces','qcs','storages','transports','outs','meetings',
            'halls','stages','talks','cooks','wcs','teas','dorms',
        );
        $resFiles = $this->service->getFacEnvJson($uid);
        if (!$resFiles) {
            foreach ($keys as $key) {
                if ($fileKey==$key) {
                    $dataArr[$key][] = $filePath;
                } else {
                    $dataArr[$key] = array();
                }
            }
            $this->service->setFacEnvByUid($uid,$dataArr);
            return true;
        }
        $resFiles = json_decode($resFiles,true);
        foreach ($keys as $key) {
            $dataArr[$key] = $resFiles[$key];
            if ($fileKey==$key) {
                $dataArr[$key][] = $filePath;
            }
        }
        $this->service->setFacEnvByUid($uid,$dataArr);
        return true;
    }

    /**
     * 企业认证，链接设置
     */
    public function setComEnvLink($uid,$datas)
    {
        foreach ($datas as $data) {
            if ($data) {
                foreach ($data as $v) { $this->setImgTmp($v); }
            }
        }
        $this->service->setFacEnvByUid($uid,$datas);
        return true;
    }

    /**
     * -------------------------------------
     * 公司环境
     * -------------------------------------
     */





    /**
     * ------------------------------------------
     * 产品
     * ------------------------------------------
     */

    /**
     * 产品列表，获取
     */
    public function getStyleList()
    {
        $cateModel = new CategoryRepository();
        return $cateModel->getSubList(146);
    }

    /**
     * 产品数据，获取
     */
    public function getOptionStyle($uid)
    {
        return $this->service->getOptionStyleList($uid);
    }

    /**
     * 产品，设置
     */
    public function setOptionStyle($uid,$data)
    {
        return $this->service->setOptionStyleList($uid,$data);
    }

    /**
     * ------------------------------------------
     * 产品
     * ------------------------------------------
     */





    /**
     * ------------------------------------------
     * 产品种类
     * ------------------------------------------
     */

    /**
     * 种类，获取
     */
    public function getOptionProType($uid)
    {
        return $this->service->getOptionProTypesList($uid);
    }

    /**
     * 种类，设置
     */
    public function setOptionProType($uid,$data)
    {
        $proTypes = $this->service->getOptionProTypeArr($uid);
        if (!$proTypes) {
            $proTypes = array($data);
        } else {
            $proTypes[] = $data;
        }
        return $this->service->setOptionProTypesList($uid,$proTypes);
    }

    /**
     * 种类，设置2修改
     */
    public function setOptionProType2($uid,$data)
    {
        return $this->service->setOptionProTypesList($uid,$data);
    }

    /**
     * ------------------------------------------
     * 产品种类
     * ------------------------------------------
     */





    /**
     * ------------------------------------------
     * 款式数量
     * ------------------------------------------
     */

    /**
     * 款式，获取
     */
    public function getOptionDesign($uid)
    {
        return $this->service->getOptionDesignList($uid);
    }

    /**
     * 款式，设置
     */
    public function setOptionDesign($uid,$data)
    {
//        return $this->service->setOptionDesignList($uid,$data);
        $designs = $this->service->getOptionDesignArr($uid);
        if (!$designs) {
            $designs = array($data);
        } else {
            $designs[] = $data;
        }
        return $this->service->setOptionDesignList($uid,$designs);
    }

    /**
     * ------------------------------------------
     * 款式
     * ------------------------------------------
     */





    /**
     * ------------------------------------------
     * 原料面料
     * ------------------------------------------
     */

    /**
     * 获取
     */
    public function getCost($uid)
    {
        $types = $this->getCostTypes($uid);
        if (!count($types)) { return ''; }
        $dataArr = array();
        foreach ($types as $k=>$type) {
            $dataArr[$k]['type']['typeName'] = $type->name;
            $models = $this->cost->getShowByType($uid,$type->id);
            if (!count($models)) { return array(); }
            foreach ($models as $k2=>$model) {
                $dataArr[$k]['subs'][$k2]['id'] = $model->id;
                $dataArr[$k]['subs'][$k2]['name'] = $model->name;
                $dataArr[$k]['subs'][$k2]['cost'] = $model->cost;
            }
        }
        return $dataArr;
    }

    /**
     * 设置
     */
    public function setCost($uid,$costs)
    {
        if (!$costs) { return ''; }
        foreach ($costs as $cost) {
            $model = $this->cost->getShowOne($cost['id']);
            if ($model) {
                $dataArr = array(
                    'name' => $cost['name'],
                    'cost' => $cost['cost'],
                    'updated_at' => time(),
                );
                $this->cost->setModify($dataArr,$cost['id']);
            } else {
                $dataArr = array(
                    'uid' => $uid,
                    'name' => $cost['name'],
                    'genre' => 1,
                    'cost' => $cost['cost'],
                    'created_at' => time(),
                );
                $this->cost->setAdd($dataArr);
            }
        }
        return true;
    }

    /**
     * 获取加工费分类
     */
    public function getCostTypes($uid)
    {
        return $this->costType->getShowByUid($uid,1);
    }

    /**
     * ------------------------------------------
     * 原料面料
     * ------------------------------------------
     */





    /**
     * 更新临时图片状态
     */
    public function setImgTmp($url)
    {
        $tmp = new UserImgTmpRepository();
        $tmp->setDelByUrl($url);
        return true;
    }
}